# -*- coding: utf8 -*-
import requests
import sys

def deployment(data):
    return requests.post('http://10.37.253.241:8000/k8s/deploy/', data=data).json()

def get_status(data):
    send_data = data
    return requests.get('http://10.37.253.241:8000/k8s/deploy/status/', send_data).json()

def main(data):
    # 部署开始
    ret = deployment(data)
    print(ret)
    if ret["code"] != 200:
        print(ret["result"])
        sys.exit(1)

    # 获取状态
    ret = get_status(data)
    print(ret)
    times = 0 #检查初使次数
    check_times = 2 #设定检查次数
    while ret['code'] != 200 and times < check_times: 
        print(ret['result'])
        ret = get_status(data)
        times += 1
    print('End: %s' % ret)
    if ret['code'] != 200:
        print(ret['result'])
        sys.exit(1)
    else:
        print(ret['result'])
        

if __name__ == '__main__':
    env = sys.argv[1]
    service_name = sys.argv[2]
    version = sys.argv[3]
    data={
        'env': env,
        'service_name': service_name,
        'version': version,
    }
    main(data)
